package com.example.scugroup8.socket;

import com.alibaba.fastjson2.JSON;
import com.example.scugroup8.mapper.ScanMapper;
import com.example.scugroup8.pojo.Scan;
import com.example.scugroup8.pojo.ServiceCheck3;
import com.example.scugroup8.pojo.ServiceCheckInfo;
import com.example.scugroup8.pojo.ServiceChecks;
import com.example.scugroup8.service.impl.ScanServiceImpl;
import com.example.scugroup8.service.impl.ServiceChecksServiceImpl;
import com.example.scugroup8.util.TimeConverter;
import org.apache.ibatis.annotations.Insert;
import org.springframework.boot.configurationprocessor.json.JSONException;
import org.springframework.boot.configurationprocessor.json.JSONObject;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.*;
import java.net.Socket;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

public class ServerThread extends Thread {

    private Socket socket;

    @Resource
    private ServiceChecksServiceImpl serviceChecksService;

    public ServerThread(Socket socket) {
        this.socket = socket;
    }

    public ServerThread(Socket socket, ServiceChecksServiceImpl serviceChecksService) {
        this.socket = socket;
        this.serviceChecksService = serviceChecksService;
    }

    @Override
    public void run() {
        InputStream is = null;
        InputStreamReader isr = null;
        BufferedReader br = null;
        OutputStream os = null;
        PrintWriter pw = null;
        try {
            is = socket.getInputStream();
            isr = new InputStreamReader(is);
            br = new BufferedReader(isr);
//            获取json格式字符串
            String s = br.readLine();
//             转化成json格式
            JSONObject jsonObject = new JSONObject(s);
//            获取json格式的typeCode属性
            String typeCode = jsonObject.getString("typeCode");
            scanType(jsonObject, typeCode);
            String scanid = jsonObject.getString("scanid");
            //对scan表插入finitime
//            InsertFinishTime(scanid);
//            System.out.println(typeCode);
//            System.out.println(jsonObject.getString("data"));
//             根据typecode执行不同的结果
            //非关闭连接 仅关闭一方的发送状况
            socket.shutdownInput();
            os = socket.getOutputStream();
            pw = new PrintWriter(os);
            pw.write("服务器欢迎你");
            pw.flush();
        } catch (Exception e) {
            e.printStackTrace();
            // TODO: handle exception
        } finally {
            //关闭资源
            try {
                if (pw != null)
                    pw.close();
                if (os != null)
                    os.close();
                if (br != null)
                    br.close();
                if (isr != null)
                    isr.close();
                if (is != null)
                    is.close();
                if (socket != null)
                    socket.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public void scanType(JSONObject jsonObject, String typeCode) throws JSONException {

//        ServiceChecksServiceImpl serviceChecksService = new ServiceChecksServiceImpl();
        ServiceChecks serviceChecks = new ServiceChecks();
        if ("1".equals(typeCode)) {

        }
        if ("2".equals(typeCode)) {

        }
        if ("3".equals(typeCode)) {

        }
        if ("4".equals(typeCode)) {

        }
        if ("5".equals(typeCode)) {
            jsonObject.remove("typeCode");
            jsonObject.remove("finish_time");
            String jsonStr = jsonObject.toString();
            ServiceCheck3 serviceCheck3 = JSON.parseObject(jsonStr, ServiceCheck3.class);
            Integer scanId = serviceCheck3.getScanid();
            String basename = serviceCheck3.getBasename();
            List<ServiceCheckInfo> date = serviceCheck3.getData();
            for (ServiceCheckInfo serviceCheckInfo : date) {
                serviceChecks.setScanId(scanId);
                serviceChecks.setBasename(basename);
                serviceChecks.setServiceCode(serviceCheckInfo.getServiceCode().toString());
                serviceChecks.setServiceShowName(serviceCheckInfo.getServiceShowName());
                serviceChecks.setServiceName(serviceCheckInfo.getServiceName());
                serviceChecks.setServiceCheck(serviceCheckInfo.getCheck());
                serviceChecks.setServiceAdvice(serviceCheckInfo.getAdvice());
                serviceChecks.setServiceResult(serviceCheckInfo.getResult());
                serviceChecks.setServiceReinforce(serviceCheckInfo.getReinforce());
                serviceChecks.setServiceExplain(serviceCheckInfo.getExplain());

//                ServiceChecksServiceImpl serviceChecksService = new ServiceChecksServiceImpl();
                serviceChecksService.insert(serviceChecks);
            }
        }
        if ("6".equals(typeCode)) {

        }
        if ("7".equals(typeCode)) {
        }
    }

    //对scan表插入finitime
    public void InsertFinishTime(String scanid) throws ParseException {
        ScanServiceImpl scanService = new ScanServiceImpl();
        //将string的时间转化成date类型
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String time = df.format(new Date());
        Date date = df.parse(time);
        Scan scan = scanService.selectById2(Integer.valueOf(scanid));
//        scan.setFinishTime(date);
        //更新scan表插入finishtime
        scanService.updateById(scan);
    }
}
